<?php

class Template extends \Eloquent {
	protected $fillable = [];



    public static function template_work($id,$openid,$data1,$data2,$data3,$data4,$data5,$data6,$data7,$link){

        $pub = Pub::find(1);

        if (Cache::has('token'))
        {
            //
            $token = Cache::get('token');
        }else{
            $token =  Wechat::get_access_token($pub->AppID,$pub->AppSecret);
            Cache::put('token', $token, 240);
        }
        $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$token;
        $template = array(
            "touser"=>$openid,
            "template_id"=>"i99GpQNJM0UFUyrQ2DQ2cgLXw53_WtiaL6sdizQ6CWM",
            "url"=>$link,
            "topcolor"=>"#FF0000",
            "data"=>array(
                'first' => array('value' => $data1,
                    'color' => '#FF0000'
                ),
                'keyword1' => array('value' => $data2,
                    'color' => '#FF0000'
                ),
                'keyword2' => array('value' => $data3,
                    'color' => '#FF0000'
                ),
                'keyword3' => array('value' => $data4,
                    'color' => '#FF0000'
                ),
                'keyword4' => array('value' => $data5,
                    'color' => '#FF0000'
                ),
                'keyword5' => array('value' => $data6,
                    'color' => '#FF0000'
                ),
                'remark' => array('value' => $data7,
                    'color' => '#FF0000'
                )
            )
        );
        $return = Wechat::http_post_json($url,json_encode($template));
        $arr = (array)json_decode($return);
        if($arr['errcode'] == 40001 || $arr['errcode'] == 42001){
            $token =  Wechat::get_access_token($pub->AppID,$pub->AppSecret);
            Cache::put('token', $token, 240);
        }else{
            if($arr['errmsg'] == 'ok'){
                $state = 1;
            }else{
                $state = $arr['errcode'];
            }
            $template = Template::find($id);
            $template->state = $state;
            $template->save();
        }
        return $arr;
    }

    public static function template_paidan($id,$openid,$data1,$data2,$data3,$data4,$data5,$data6,$data7,$link){

        $pub = Pub::find(1);

        if (Cache::has('token'))
        {
            //
            $token = Cache::get('token');
        }else{
            $token =  Wechat::get_access_token($pub->AppID,$pub->AppSecret);
            Cache::put('token', $token, 240);
        }
        $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$token;
        $template = array(
            "touser"=>$openid,
            "template_id"=>"NfHuPFlYRgV_b8GEZ3apNowURBM536L4_jAtbnYa4aE",
            "url"=>$link,
            "topcolor"=>"#FF0000",
            "data"=>array(
                'first' => array('value' => $data1,
                    'color' => '#FF0000'
                ),
                'keyword1' => array('value' => $data2,
                    'color' => '#FF0000'
                ),
                'keyword2' => array('value' => $data3,
                    'color' => '#FF0000'
                ),
                'keyword3' => array('value' => $data4,
                    'color' => '#FF0000'
                ),
                'keyword4' => array('value' => $data5,
                    'color' => '#FF0000'
                ),
                'keyword5' => array('value' => $data6,
                    'color' => '#FF0000'
                ),
                'remark' => array('value' => $data7,
                    'color' => '#FF0000'
                )
            )
        );
        $return = Wechat::http_post_json($url,json_encode($template));
        $arr = (array)json_decode($return);
        if($arr['errcode'] == 40001 || $arr['errcode'] == 42001){
            $token =  Wechat::get_access_token($pub->AppID,$pub->AppSecret);
            Cache::put('token', $token, 240);
        }else{
            if($arr['errmsg'] == 'ok'){
                $state = 1;
            }else{
                $state = $arr['errcode'];
            }
            $template = Template::find($id);
            $template->state = $state;
            $template->save();
        }
        return $arr;
    }

    public static function template_complete($id,$openid,$data1,$data2,$data3,$data4,$link){

        $pub = Pub::find(1);

        if (Cache::has('token'))
        {
            //
            $token = Cache::get('token');
        }else{
            $token =  Wechat::get_access_token($pub->AppID,$pub->AppSecret);
            Cache::put('token', $token, 240);
        }
        $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$token;
        $template = array(
            "touser"=>$openid,
            "template_id"=>"BSeautp57sOZq96x1YEzWv2z41l31GueCmWg0P5zGhc",
            "url"=>$link,
            "topcolor"=>"#FF0000",
            "data"=>array(
                'first' => array('value' => $data1,
                    'color' => '#FF0000'
                ),
                'keynote1' => array('value' => $data2,
                    'color' => '#FF0000'
                ),
                'keynote2' => array('value' => $data3,
                    'color' => '#FF0000'
                ),
                'remark' => array('value' => $data4,
                    'color' => '#FF0000'
                )
            )
        );
        $return = Wechat::http_post_json($url,json_encode($template));
        $arr = (array)json_decode($return);
        if($arr['errcode'] == 40001 || $arr['errcode'] == 42001){
            $token =  Wechat::get_access_token($pub->AppID,$pub->AppSecret);
            Cache::put('token', $token, 240);
        }else{
            if($arr['errmsg'] == 'ok'){
                $state = 1;
            }else{
                $state = $arr['errcode'];
            }
            $template = Template::find($id);
            $template->state = $state;
            $template->save();
        }
        return $arr;
    }
    public static function template_daoqi($id,$openid,$data1,$data2,$data3,$data4,$link){

        $pub = Pub::find(1);

        if (Cache::has('token'))
        {
            //
            $token = Cache::get('token');
        }else{
            $token =  Wechat::get_access_token($pub->AppID,$pub->AppSecret);
            Cache::put('token', $token, 240);
        }
        $url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$token;
        $template = array(
            "touser"=>$openid,
            "template_id"=>"F6sZbVxi0I9Rz9cCN9yuO1HC9O2rpyyjttjjPM166T4",
            "url"=>$link,
            "topcolor"=>"#FF0000",
            "data"=>array(
                'first' => array('value' => $data1,
                    'color' => '#FF0000'
                ),
                'keyword1' => array('value' => $data2,
                    'color' => '#FF0000'
                ),
                'keyword2' => array('value' => $data3,
                    'color' => '#FF0000'
                ),
                'remark' => array('value' => $data4,
                    'color' => '#FF0000'
                )
            )
        );
        $return = Wechat::http_post_json($url,json_encode($template));
        $arr = (array)json_decode($return);
        if($arr['errcode'] == 40001 || $arr['errcode'] == 42001){
            $token =  Wechat::get_access_token($pub->AppID,$pub->AppSecret);
            Cache::put('token', $token, 240);
        }else{
            if($arr['errmsg'] == 'ok'){
                $state = 1;
            }else{
                $state = $arr['errcode'];
            }
            $template = Template::find($id);
            $template->state = $state;
            $template->save();
        }
        return $arr;
    }

    public static function newone($openid,$data,$url,$type){
        $template = new Template();
        $template->openid = $openid;
        foreach($data as $key => $val){
            $i = $key+1;
            $name = 'data'.$i;
            $template->$name = $val;
        }
        $template->url = $url;
        $template->type = $type;
        $template->state = 0;
        $template->save();
        return $template->id;
    }
}